﻿//RtsHelper.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Algo.History.Rts
{
	using System;
	using System.IO;

	using DotNetDBF;

	using Ecng.Common;
	using Ecng.Collections;

	using Ionic.Zip;

	using LumiSoft.Net.FTP.Client;

	static class RtsHelper
	{
		public static int GetFieldIndex(this DBFReader reader, string fieldName)
		{
			return reader.Fields.IndexOf(f => f.Name == fieldName);
		}

		public static Stream DownloadFile(this FTP_Client client, string remoteDir, string fileName, string localFile)
		{
			var stream = new MemoryStream();
			client.GetFile(Path.Combine(remoteDir, fileName), stream);
			File.WriteAllBytes(localFile, stream.To<byte[]>());
			return stream;
		}

		public static void Extract(this Stream stream, string localPath)
		{
			if (stream == null)
				throw new ArgumentNullException("stream");

			stream.Position = 0;

			using (var zip = ZipFile.Read(stream))
				zip.ExtractAll(localPath, ExtractExistingFileAction.OverwriteSilently);
		}

		public static void Extract(this string zipFile, string destFolder)
		{
			using (var zip = ZipFile.Read(zipFile))
				zip.ExtractAll(destFolder, ExtractExistingFileAction.OverwriteSilently);
		}
	}
}